<?php

namespace app\adminapi\controller;

use think\Controller;
use think\Request;
use app\common\model\Auth as AuthModel;

class Auth extends BaseApi
{
    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //接受参数 keyword type
        $param = input();
        $where = [];
        if (!empty($param['keyword'])){
            $where['auth_name'] = ['like',"%{$param['keyword']}%"];
        }
        //查询数据
        $list = AuthModel::where($where)->select();
        $list = (new \think\Collection($list))->toArray();
        if (!empty($param['type']) && $param['type'] == 'tree'){
            $list = get_tree_list($list);
        }else{
            $list = get_cate_list($list);
        }
        $this->ok($list);
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $params = input();
        if (empty($params['pid'])){
            $params = 0;
        }
        if (empty($params['is_nav'])){
            $params['is_nav'] = $params['radio'];
        }
        $validate = $this->validate($params,[
            'auth_name|权限名称'=>'require',
            'pid|上级权限' => 'require',
            'is_nav|菜单权限' => 'require',
        ]);
        if ($validate !== true){
            $this->fail($validate,504);
        }
        //添加数据（是否顶级，级别和pid_path处理）
        if($params['pid'] == 0){
            $params['level'] = 0;
            $params['pid_path'] = 0;
            $params['auth_c'] = '';
            $params['auth_a'] = '';
        }else{
            //不是顶级权限
            //查询上级信息
            //$p_info = \app\common\model\Auth::where('id', $params['pid'])->find();
            $p_info = \app\common\model\Auth::find($params['pid']);
            if(empty($p_info)){
                $this->fail('数据异常');
            }
            //设置级别+1  家族图谱 拼接
            $params['level'] = $p_info['level'] + 1;
            $params['pid_path'] = $p_info['pid_path'] . '_' . $p_info['id'];
        }
        //实际开发 可能不需要返回新增的这条数据
        //\app\common\model\Auth::create($params, true);
        //$this->ok();
        //restful 严格风格
        $auth = \app\common\model\Auth::create($params, true);
        $info = \app\common\model\Auth::find($auth['id']);
        //返回数据
        $this->ok($info);
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        $auth = AuthModel::field('id,auth_name,pid,pid_path,auth_c,auth_a,is_nav,level')-> find($id);
        $this->ok($auth);
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        $params = input();
        if (empty($params['pid'])){
            $params = 0;
        }
        if (empty($params['is_nav'])){
            $params['is_nav'] = $params['radio'];
        }
        $validate = $this->validate($params,[
            'auth_name|权限名称'=>'require',
            'pid|上级权限' => 'require',
            'is_nav|菜单权限' => 'require',
        ]);
        if ($validate !== true){
            $this->fail($validate,504);
        }
        //修改的数据 是否是顶级，级别和pid_paht处理
        $auth = AuthModel::find($id);
        if ($params['pid'] == 0){
            $params['level'] = 0;
            $params['pid_path'] = 0;
            $params['auth_c'] = '';
            $params['auth_a'] = '';
        }
        else if ($params['pid'] != $auth['pid']){
            $p_auth = AuthModel::find($params['pid']);
            if (empty($p_auth)){
                $this->fail('数据异常',505);
            }
            $params['level'] = $p_auth['level'] + 1;
            $params['p_auth'] = $p_auth['p_auth'].'_'.$p_auth['id'];
        }
        $info = AuthModel::update($params, ['id'=>$id],true);
        if ($info){
            $row = AuthModel::find($id);
            $this->ok($row);
        }else{
            $this->fail('修改失败',506);
        }
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //如果有子权限不能删除
        $auth = AuthModel::where('pid',$id)->count();
        if ($auth > 0){
            $this->fail('有子权限不能删除');
        }
        $bool = AuthModel::destroy($id);
        if ($bool){
            $this->ok();
        }else{
            $this->fail();
        }

    }
    /*
     * 菜单栏
     */
    public  function nav(){
        $user_id = input('user_id');
        //查询管理员角色id
        //判断是否是超级管理
        $user_info = \app\common\model\Admin::find($user_id);
        $role_id = $user_info['role_id'];
        if ($role_id == 1){
            $data = AuthModel::where('is_nav',1)->select();
        }else{
            //先查角色信息
            $role = \app\common\model\Role::find($user_info['role_id']);
            $role_auth_ids = $role['role_auth_ids'];
            $data = AuthModel::where('is_nav',1)->where('id','id',$role_auth_ids)->select();
        }
        $data = (new \think\Collection($data))->toArray();
        $data = get_cate_list($data);
        $this->ok($data);
    }
}
